/*
 * LagrangeInterpolatorTest.cpp
 *
 *  Created on: 23 Aug 2011
 *      Author: Allan
 */

#include "LagrangeInterpolatorTest.h"

// C++ includes
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

// Boost includes
#include <boost/foreach.hpp>

// GeoReact includes
#include "../../Utilities/Range.h"
#include "../../Numerics/LagrangeInterpolator.h"
#include "../../Numerics/LinearInterpolator.h"

void LagrangeInterpolatorTest()
{
	vector<double> xi = Range(0.0, 2.0, 0.25);
	vector<double> yi = xi;
	
	std::transform(yi.begin(), yi.end(), yi.begin(), static_cast<double(*)(double)>(std::exp));
	
	LinearInterpolator lin(xi, yi);
	LagrangeInterpolator li1(xi, yi, 1), li2(xi, yi, 2);
	
	cout << setw(10) << left << "x";
	cout << setw(10) << left << "lin(x)";
	cout << setw(10) << left << "li1(x)";
	cout << setw(10) << left << "li2(x)";
	cout << setw(10) << left << "exp(x)";
	cout << setw(10) << left << "abs(elin)";
	cout << setw(10) << left << "abs(e1)";
	cout << setw(10) << left << "abs(e2)";
	cout << endl;
	
	cout << fixed << setprecision(6);
	
	BOOST_FOREACH(double x, Range(-1.0, 3.0, 0.1))
	{
		cout << setw(10) << left << x;
		cout << setw(10) << left << lin(x);
		cout << setw(10) << left << li1(x);
		cout << setw(10) << left << li2(x);
		cout << setw(10) << left << std::exp(x);
		cout << setw(10) << left << std::abs(lin(x) - std::exp(x));
		cout << setw(10) << left << std::abs(li1(x) - std::exp(x));
		cout << setw(10) << left << std::abs(li2(x) - std::exp(x));
		cout << endl;
	}
}
